
import {size, formItemLayout, getImages, getFileList } from '../../utils/common';
import React from 'react';
import { Modal, Form, Input, Select } from 'antd';
import * as api from '../../api/api';
import { FormComponentProps } from 'antd/lib/form';
import Uploads from '../Uploads';
export interface ComplainEditProps extends FormComponentProps {
    visible: boolean,
    complain?: api.SingleUserMallComplainsVOComplainViewVO,
    onSave: (any) => void,
    onCancel: () => void,
}
class ComplainEdit extends React.PureComponent<ComplainEditProps, any>{
    constructor(props) {
        super(props);
    }

    state = {
        pictures: []
    }
    
    componentDidMount() {
        const pictures = this.props.complain && this.props.complain.picture;
        this.setState({ pictures: getFileList(pictures) });
    }

    ok = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, form) => {
            if (!err) {
                form['picture'] = getImages(this.state.pictures);
                this.props.onSave(form);
            }
        });
    }

    cancel = () => {
        this.props.onCancel();
    }

    pictureChange = (pictures) => {
        this.setState({ pictures });
    }
    

    render(): JSX.Element {
        const { getFieldDecorator } = this.props.form;
        const { complain } = this.props;
        return (
            <Modal
                title='投诉信息'
                visible={this.props.visible}
                onOk={this.ok}
                onCancel={this.cancel}
                maskClosable={true}
            >
                <Form>
                    <Form.Item {...formItemLayout} >
                        {getFieldDecorator('id', {
                            initialValue: complain && complain.id
                        })(<Input size={size} type="hidden" />)}
                    </Form.Item>
                    <Form.Item label="投诉人" {...formItemLayout}>
                        {getFieldDecorator('name', {
                            rules: [
                                {
                                    required: true,
                                    message: '请输入姓名'
                                }
                            ],
                            initialValue: complain && complain.userName
                        })(<Input size={size} 
                            placeholder="请输入姓名" />)}
                    </Form.Item>
                    <Form.Item label="描述" {...formItemLayout}>
                        {getFieldDecorator('description', {
                            rules: [
                                {
                                    required: true,
                                    message: '请输入描述'
                                }
                            ],
                            initialValue: complain && complain.description
                        })(<Input size={size} height="50px"
                            placeholder="请输入描述" />)}
                    </Form.Item>
                    <Form.Item label="图片" {...formItemLayout}>
                        {getFieldDecorator('picture', {
                            rules: [{
                                required: true,
                                message: '请上传图片'
                            }
                            ],
                            initialValue: complain && complain.picture
                        })(
                            < Uploads editable={true} fileList={this.state.pictures} onChange={this.pictureChange} />)}
                    </Form.Item> 
                    <Form.Item label='投诉类型' {...formItemLayout}>
                        {getFieldDecorator('complainTypeView', {
                            initialValue: complain && complain.complainTypeView
                        })(
                            <Input size={size}
                                placeholder="" />)}
                    </Form.Item>
                    <Form.Item label='回复' {...formItemLayout}>
                        {getFieldDecorator('remark', {
                            initialValue: complain && complain.remark
                        })(
                            <Input.TextArea
                                placeholder="请输入回复内容" />)}
                    </Form.Item>
                    <Form.Item label="是否通过" {...formItemLayout}>
                        {getFieldDecorator('isPass', {
                            rules: [
                                {
                                    required: true,
                                    message: '请选择'
                                }
                            ],
                            initialValue: 'true'
                        })(<Select
                            placeholder="请选择"
                        >
                            <Select.Option key='true'>审核通过</Select.Option>
                            <Select.Option key='false'>审核驳回</Select.Option>
                        </Select>)}
                    </Form.Item>
                </Form>
            </Modal>)
    }
}

export default Form.create()(ComplainEdit);